--功能：驱动CH455G，改变某RGB灯的颜色。

module(...,package.seeall)

require"misc"

local i2cid=2

--比如：LED1的R颜色打开，需要传入参数（"LED1","R",1）
--（"LED2","G",0）表示
local DIG0 = 0 --DIG0的数据缓存
local DIG1 = 0 --DIG1的数据缓存
local Lighteness = 0x11 --整体亮度的数据缓存，是控制寄存器的缓存。
function ShowRGB(LED_X,RGB_X,SWITCH)
    if i2c.setup(i2cid,i2c.SLOW) ~= i2c.SLOW then
        print("ShowRGB Init IIC fail")
        return
    end
    i2c.send(i2cid, 0x24, Lighteness) --手册中写的地址是0X48，标准IIC地址需要除以2。打开显示，设置亮度等级为1/8 。
    if LED_X=="LED1" then 
        if RGB_X=="R" then
            if SWITCH >= 1 then
                DIG0 = bit.set(DIG0,2) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG0 = bit.clear(DIG0,2) --相应的位数置0
            end
        end
        if RGB_X=="G" then
            if SWITCH >= 1 then
                DIG0 = bit.set(DIG0,1) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG0 = bit.clear(DIG0,1) --相应的位数置0
            end
        end
        if RGB_X=="B" then
            if SWITCH >= 1 then
                DIG0 = bit.set(DIG0,0) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG0 = bit.clear(DIG0,0) --相应的位数置0
            end
        end
    end
    if LED_X=="LED2" then 
        if RGB_X=="R" then
            if SWITCH >= 1 then
                DIG0 = bit.set(DIG0,5) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG0 = bit.clear(DIG0,5) --相应的位数置0
            end
        end
        if RGB_X=="G" then
            if SWITCH >= 1 then
                DIG0 = bit.set(DIG0,4) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG0 = bit.clear(DIG0,4) --相应的位数置0
            end
        end
        if RGB_X=="B" then
            if SWITCH >= 1 then
                DIG0 = bit.set(DIG0,3) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG0 = bit.clear(DIG0,3) --相应的位数置0
            end
        end
    end
    if LED_X=="LED4" then 
        if RGB_X=="R" then
            if SWITCH >= 1 then
                DIG1 = bit.set(DIG1,2) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG1 = bit.clear(DIG1,2) --相应的位数置0
            end
        end
        if RGB_X=="G" then
            if SWITCH >= 1 then
                DIG1 = bit.set(DIG1,1) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG1 = bit.clear(DIG1,1) --相应的位数置0
            end
        end
        if RGB_X=="B" then
            if SWITCH >= 1 then
                DIG1 = bit.set(DIG1,0) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG1 = bit.clear(DIG1,0) --相应的位数置0
            end
        end
    end
    if LED_X=="LED3" then 
        if RGB_X=="R" then
            if SWITCH >= 1 then
                DIG1 = bit.set(DIG1,5) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG1 = bit.clear(DIG1,5) --相应的位数置0
            end
        end
        if RGB_X=="G" then
            if SWITCH >= 1 then
                DIG1 = bit.set(DIG1,4) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG1 = bit.clear(DIG1,4) --相应的位数置0
            end
        end
        if RGB_X=="B" then
            if SWITCH >= 1 then
                DIG1 = bit.set(DIG1,3) --相应的位数置1
            end
            if SWITCH == 0 then
                DIG1 = bit.clear(DIG1,3) --相应的位数置0
            end
        end
    end
    --log.info("DIG1 = ",DIG1)
    i2c.send(i2cid, 0x34, DIG0) --手册中的0X68，对应DIG0
    i2c.send(i2cid, 0x35, DIG1) --手册中的0X6A，对应DIG1
    i2c.close(i2cid)
end

--调整整体亮度
function Set_Lighteness(Light)
    if i2c.setup(i2cid,i2c.SLOW) ~= i2c.SLOW then
        print("Set_Lighteness Init IIC fail")
        return
    end
    if Light == 0 then
        Lighteness =  0x10
    end
    if Light == 1 then
        Lighteness = 0x11
    end
    if Light == 2 then
        Lighteness = 0x21
    end
    if Light == 3 then
        Lighteness = 0x31
    end
    if Light == 4 then
        Lighteness = 0x41
    end
    if Light == 5 then
        Lighteness = 0x51
    end
    if Light == 6 then
        Lighteness = 0x61
    end
    if Light == 7 then
        Lighteness = 0x71
    end
    if Light == 8 then
        Lighteness = 0x01
    end
    i2c.send(i2cid, 0x24, Lighteness) --手册中写的地址是0X48，标准IIC地址需要除以2。打开显示，设置亮度等级
    i2c.send(i2cid, 0x34, DIG0) --手册中的0X68，对应DIG0
    i2c.send(i2cid, 0x35, DIG1) --手册中的0X6A，对应DIG1
    i2c.close(i2cid)
end

--返回一次ADC的原始数值 ADC原始值最大是740
function ReadADC()
    adc.open(0)
    local val,mv = adc.read(0)
    adc.close(0)
    log.info("ADC= ",val)
    --log.info("mv= ",mv/3)
    return 800-val
end

--返回当前的RGB状态，JSON格式，用于上报云平台
function Read_RGB()
    local RGB1={}
    local RGB2={}
    local RGB3={}
    local RGB4={}
    if bit.isset(DIG0,0) then RGB1.B=1 else RGB1.B=0 end
    if bit.isset(DIG0,1) then RGB1.G=1 else RGB1.G=0 end
    if bit.isset(DIG0,2) then RGB1.R=1 else RGB1.R=0 end

    if bit.isset(DIG0,3) then RGB2.B=1 else RGB2.B=0 end
    if bit.isset(DIG0,4) then RGB2.G=1 else RGB2.G=0 end
    if bit.isset(DIG0,5) then RGB2.R=1 else RGB2.R=0 end

    if bit.isset(DIG1,0) then RGB4.B=1 else RGB4.B=0 end
    if bit.isset(DIG1,1) then RGB4.G=1 else RGB4.G=0 end
    if bit.isset(DIG1,2) then RGB4.R=1 else RGB4.R=0 end

    if bit.isset(DIG1,3) then RGB3.B=1 else RGB3.B=0 end
    if bit.isset(DIG1,4) then RGB3.G=1 else RGB3.G=0 end
    if bit.isset(DIG1,5) then RGB3.R=1 else RGB3.R=0 end

    local RGB={}
    RGB.RGB1 = RGB1
    RGB.RGB2 = RGB2
    RGB.RGB3 = RGB3
    RGB.RGB4 = RGB4

    if Lighteness == 0x10 then 
        RGB.Lighteness = 0
    elseif Lighteness == 0x01 then 
        RGB.Lighteness = 8
    else
        RGB.Lighteness = bit.rshift(Lighteness,4)
    end
    
    log.info("Read_RGB=",json.encode(RGB))

    return RGB
end

--低电压关灯 电压小于3.51V时关闭RGB灯.
local function LowPower()
    if misc.getVbatt()<3510 then
        Set_Lighteness(0)   --关闭RGB
        log.info("LowPower=",misc.getVbatt())
        pmd.sleep(1)    --进入休眠模式
    end
end
sys.timerLoopStart(LowPower,10000)

--测试一颗RGB用
function Test_RGB(LED_X)
    ShowRGB(LED_X,"R",1)
    rtos.sleep(500)
    ShowRGB(LED_X,"R",0)
    rtos.sleep(500)
    ShowRGB(LED_X,"G",1)
    rtos.sleep(500)
    ShowRGB(LED_X,"G",0)
    rtos.sleep(500)
    ShowRGB(LED_X,"B",1)
    rtos.sleep(500)
    ShowRGB(LED_X,"B",0)
    rtos.sleep(500)
    ShowRGB(LED_X,"R",1)
    rtos.sleep(500)
    ShowRGB(LED_X,"G",1)
    rtos.sleep(500)
    ShowRGB(LED_X,"B",1)
    rtos.sleep(500)
    ShowRGB(LED_X,"R",0)
    rtos.sleep(500)
    ShowRGB(LED_X,"G",0)
    rtos.sleep(500)
    ShowRGB(LED_X,"B",0)
    rtos.sleep(500)
end
--测试全部RGB用
function Test_All_RGB()
    ShowRGB("LED1","R",1)
    ShowRGB("LED2","R",1)
    ShowRGB("LED3","R",1)
    ShowRGB("LED4","R",1)
    rtos.sleep(500)
    ShowRGB("LED1","R",0)
    ShowRGB("LED2","R",0)
    ShowRGB("LED3","R",0)
    ShowRGB("LED4","R",0)
    rtos.sleep(500)
    ShowRGB("LED1","G",1)
    ShowRGB("LED2","G",1)
    ShowRGB("LED3","G",1)
    ShowRGB("LED4","G",1)
    rtos.sleep(500)
    ShowRGB("LED1","G",0)
    ShowRGB("LED2","G",0)
    ShowRGB("LED3","G",0)
    ShowRGB("LED4","G",0)
    rtos.sleep(500)
    ShowRGB("LED1","B",1)
    ShowRGB("LED2","B",1)
    ShowRGB("LED3","B",1)
    ShowRGB("LED4","B",1)
    rtos.sleep(500)
    ShowRGB("LED1","B",0)
    ShowRGB("LED2","B",0)
    ShowRGB("LED3","B",0)
    ShowRGB("LED4","B",0)
end

--Test_RGB("LED1")
--Test_RGB("LED2")
--Test_RGB("LED3")
--Test_RGB("LED4")
--sys.timerLoopStart(ReadADC,10000) --测试用，周期打印ADC数值

Test_All_RGB()


